package com.delio.client.pagos.pagosueldos;

import java.util.Date;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.pagos.ingresos.PanelGridPagoIngreso;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

public class PanelNuevoPagoSueldo extends FormPanel {

	private HiddenField<String> id;
	private NumberField txtAportes;
	private NumberField txtTardanzas;
	private NumberField txtAdelantos;
	private NumberField txtExtras;
	private NumberField txtFaltas;
	private NumberField txtOtros;
	private NumberField txtNeto;
	private NumberField txtTotal;
	private NumberField txtSueldo;
	private NumberField txtSaldo;
	private DateField fecha;
	private FieldSet fsdescto, fsingreso, fspago, fsdsctoservidor;

	public  Button btnRegistrar;
	private FormBinding formbinding;

	private DelioConstants k = GWT.create(DelioConstants.class);

	public PanelNuevoPagoSueldo() {
		super();
		setScrollMode(Scroll.AUTOX);
		setHeaderVisible(false);
		setFrame(true);
		setPadding(0);
		setStyleAttribute("marginTop", "3px");

		PanelGridPagoSueldo.panelnuevopagosueldo=this;
		
		formbinding=new FormBinding(this,false);
		id = new HiddenField<String>();

		btnRegistrar = new Button(k.registrar());
		btnRegistrar.setSize(100, 42);
		btnRegistrar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
			save();
				
			}
		});

		fecha = new DateField();
		fecha.setFieldLabel(k.fechapago());
		fecha.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		fecha.setValue(new Date());
		
		
	

		LayoutContainer todo = new LayoutContainer();

		LayoutContainer panel = new LayoutContainer();
		panel.setLayout(new ColumnLayout());

		LayoutContainer boton = new LayoutContainer();
		boton.setStyleAttribute("marginLeft", "80%");
		boton.setStyleAttribute("marginTop", "10px");
		boton.setStyleAttribute("marginRight", "12px");
		boton.setLayout(new ColumnLayout());

		LayoutContainer lc01 = new LayoutContainer();
		lc01.setLayout(new FormLayout(LabelAlign.TOP));
		lc01.add(fecha, new FormData("90%"));

		LayoutContainer lc02 = new LayoutContainer();
		lc02.setLayout(new FormLayout());
		lc02.add(btnRegistrar, new FormData("98%"));

		boton.add(lc01, new ColumnData(.6));
		boton.add(lc02, new ColumnData(.4));

		LayoutContainer lc1 = new LayoutContainer();
		lc1.setStyleAttribute("marginLeft", "20px");
		lc1.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc2 = new LayoutContainer();
		lc2.setStyleAttribute("marginLeft", "5px");
		lc2.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc3 = new LayoutContainer();
		lc3.setStyleAttribute("marginLeft", "15px");
		lc3.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc4 = new LayoutContainer();
		lc4.setStyleAttribute("marginLeft", "5px");
		lc4.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc5 = new LayoutContainer();
		lc5.setStyleAttribute("marginLeft", "15px");
		lc5.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc6 = new LayoutContainer();
		lc6.setStyleAttribute("marginLeft", "20px");
		lc6.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc7 = new LayoutContainer();
		lc7.setStyleAttribute("marginLeft", "20px");
		lc7.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc8 = new LayoutContainer();
		lc8.setStyleAttribute("marginLeft", "15px");
		lc8.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc9 = new LayoutContainer();
		lc9.setStyleAttribute("marginLeft", "20px");
		lc9.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc10 = new LayoutContainer();
		lc10.setStyleAttribute("marginLeft", "20px");
		lc10.setLayout(new FormLayout(LabelAlign.TOP));

		txtAportes = new NumberField();
		txtAportes.setFieldLabel(k.descuento());
		txtAportes.setPropertyEditorType(Double.class);
		lc1.add(txtAportes, new FormData("70%"));

		txtTardanzas = new NumberField();
		txtTardanzas.setFieldLabel(k.tardanzas());
		txtTardanzas.setPropertyEditorType(Double.class);
		lc2.add(txtTardanzas, new FormData("70%"));

		txtExtras = new NumberField();
		txtExtras.setFieldLabel(k.extras());
		txtExtras.setPropertyEditorType(Double.class);
		lc3.add(txtExtras, new FormData("70%"));

		txtFaltas = new NumberField();
		txtFaltas.setFieldLabel(k.faltas());
		txtFaltas.setPropertyEditorType(Double.class);
		lc4.add(txtFaltas, new FormData("70%"));

		txtOtros = new NumberField();
		txtOtros.setFieldLabel(k.otros());
		txtOtros.setPropertyEditorType(Double.class);
		txtOtros.addListener(Events.OnFocus, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				
					new PanelGridPagoIngreso().show();
				
				
				
			}
		});
		
		lc5.add(txtOtros, new FormData("70%"));

		txtNeto = new NumberField();
		txtNeto.setFieldLabel(k.neto());
		txtNeto.setPropertyEditorType(Double.class);
		lc6.add(txtNeto, new FormData("70%"));

		txtTotal = new NumberField();
		txtTotal.setFieldLabel(k.total());
		txtTotal.setPropertyEditorType(Double.class);
		lc7.add(txtTotal, new FormData("70%"));

		txtSueldo = new NumberField();
		txtSueldo.setFieldLabel(k.sueldo());
		txtSueldo.setPropertyEditorType(Double.class);
		lc8.add(txtSueldo, new FormData("70%"));

		txtSaldo = new NumberField();
		txtSaldo.setFieldLabel(k.saldo());
		txtSaldo.setPropertyEditorType(Double.class);
		lc9.add(txtSaldo, new FormData("70%"));

		txtAdelantos = new NumberField();
		txtAdelantos.setFieldLabel(k.adelanto());
		txtAdelantos.setPropertyEditorType(Double.class);
		lc10.add(txtAdelantos, new FormData("70%"));

		fsingreso = new FieldSet();
		fsingreso.setHeading(k.ingresos());
		fsingreso.setHeight(100);
		fsingreso.setStyleAttribute("marginLeft", "10px");
		fsingreso.setStyleAttribute("marginTop", "10px");
		fsingreso.setLayout(new ColumnLayout());
		fsingreso.add(lc8, new ColumnData());
		fsingreso.add(lc3, new ColumnData());
		fsingreso.add(lc5, new ColumnData());

		fsdescto = new FieldSet();
		fsdescto.setStyleAttribute("marginLeft", "10px");
		fsdescto.setStyleAttribute("marginTop", "10px");
		fsdescto.setHeight(100);
		fsdescto.setHeading(k.otrosdsctos());
		fsdescto.setLayout(new ColumnLayout());
		fsdescto.add(lc2, new ColumnData());
		fsdescto.add(lc4, new ColumnData());

		fsdsctoservidor = new FieldSet();
		fsdsctoservidor.setHeading(k.descuentoservidor());
		fsdsctoservidor.setHeight(100);
		fsdsctoservidor.setStyleAttribute("marginLeft", "10px");
		fsdsctoservidor.setStyleAttribute("marginTop", "10px");
		fsdsctoservidor.setLayout(new ColumnLayout());
		fsdsctoservidor.add(lc1, new ColumnData(1));

		fspago = new FieldSet();
		fspago.setHeading(k.sueldototal());
		fspago.setHeight(100);
		fspago.setStyleAttribute("marginLeft", "10px");
		fspago.setStyleAttribute("marginTop", "10px");
		fspago.setLayout(new ColumnLayout());
		fspago.add(lc7, new ColumnData());
		fspago.add(lc6, new ColumnData());
		fspago.add(lc10, new ColumnData());
		fspago.add(lc9, new ColumnData());

		add(id);
		panel.add(fsingreso, new ColumnData(.28));
		panel.add(fsdescto, new ColumnData(.17));
		panel.add(fsdsctoservidor, new ColumnData(.12));
		panel.add(fspago, new ColumnData(.39));

		todo.add(panel);
		todo.add(boton);

		add(todo);
		
		setStore(PagoSueldos.store);
	}

	public void setStore(ListStore<BeanModel> store) {

		formbinding.addFieldBinding(new FieldBinding(id, "id"));
		formbinding
				.addFieldBinding(new FieldBinding(txtAdelantos, "adelantos"));
		formbinding.addFieldBinding(new FieldBinding(txtAportes, "aportes"));
		formbinding.addFieldBinding(new FieldBinding(txtExtras, "extras"));
		formbinding.addFieldBinding(new FieldBinding(txtFaltas, "faltas"));
		formbinding.addFieldBinding(new FieldBinding(txtNeto, "neto"));
		formbinding.addFieldBinding(new FieldBinding(txtOtros, "otros"));
		formbinding.addFieldBinding(new FieldBinding(txtSaldo, "saldo"));
		formbinding.addFieldBinding(new FieldBinding(txtSueldo, "sueldo"));
		formbinding.addFieldBinding(new FieldBinding(txtTardanzas, "tardanzas"));
		formbinding.addFieldBinding(new FieldBinding(txtTotal, "total"));
		formbinding.setStore(store);
		
		setHabilitar(false);
	}

	@Override
	public void reset() {

		id.clear();
		txtAdelantos.clear();
		txtAportes.clear();
		txtExtras.clear();
		txtFaltas.clear();
		txtSueldo.clear();
		txtNeto.clear();
		txtOtros.clear();
		txtSaldo.clear();
		txtTardanzas.clear();
		txtTotal.clear();
		fecha.clear();

		PanelGridPagoSueldo.grid.getSelectionModel().deselectAll();
		unbind();

	}

	public void bind(BeanModel selectedModel) {
		formbinding.bind(selectedModel);
	setHabilitar(true);

	}

	public void unbind() {
		formbinding.unbind();
		setHabilitar(false);
	
	}

	
	private void save(){
	PagoSueldo ps =new PagoSueldo();
	ps.setFecha(fecha.getValue());
	PagoSueldos.Save(ps);
		
		
	}
	
	private void setHabilitar(Boolean valor){
		setEnabled(valor);
	}
}
